﻿///<reference path="jquery-1.3.2-vsdoc2.js" />
/*
Copyright (c) 2010 ZTE  Qiaoxufeng
*/

(function($) {
    $.fn.toolbar = function(settings) {
        settings = $.extend( settings || {});
        
        var theElement = this;
        theElement.addClass("toolbar");
        
        $('ul.toolbar li ul').hide();
        
        //缓存对象
        var $childItem = theElement.find("div.ribbon-list div");
        
        /*为菜单项添加hover需效果*/
        hoverHanlder('li div','item');
        hoverHanlder('div.ribbon-list','itemlist');
        hoverHanlder('div.ribbon-list div','childitem');
        hoverHanlder('div.ribbon-list ul li','menuitem');
        
        function hoverHanlder(obj,clsname){
        	theElement.find(obj).each(function() {
        		$(this).addClass(clsname).hover(function(){
        			$(this).toggleClass(clsname+'hover');
        		});
            });
        }
      
      //qiaoxufeng::it's necessary to place arrow icon before other elements,there are bugs for using 'float' attribute!
      var arrowMenu = '<img src="'+settings.rootPath+'images/arrow_down.png" style="float: right;margin-top: 5px" />';    
      $childItem.each(function() {
        	if ($(this).children('ul').length > 0) { 
        		$(this).find(':first').before(arrowMenu) } }
        );
      //Hack for IE 7 and 6,we must set width,otherwise,'float' atrribute is invalidate
      if (navigator.appVersion.indexOf('MSIE 6.0') > -1 || navigator.appVersion.indexOf('MSIE 7.0') > -1)      
      {
    	  //theElement.find('li div').css('width', '90px');
    	  //theElement.find('li div.ribbon-list').css('width', '100px');
    	  $childItem.each(function(){
    		  var v = $(this).css('width');
    		  if(v==null || v=="" || v=="auto") {$(this).css('width', '70px');} //for ie6/7's bug
    	  });    	      	     	
       }     
      
      theElement.find('.ribbon-list ul li').each(function(){
    	  var $icon = $(this).find('img');
		  var w=0;
		  if($icon.length==1){
				$icon.attr('border','0');//.css('width','20px').css('height','20px');				
				w+=20;
		  }
		  
		  if($(this).find('span').length<=0){
    		  var text = $(this).text();
    		  $(this).text('').append('<span>'+text+'</span>');  
    	  }
		  $(this).find('SPAN').css('padding-left',28-w);
      });
      
      $childItem.click(function(e) {
    	  if($(this).children('ul').length<=0) return;
    	  
          var elwidth = $(this).parent().width();
          var insideX = e.pageX > $(this).offset().left && e.pageX < $(this).offset().left + $(this).width();
          var insideY = e.pageY > $(this).offset().top && e.pageY < $(this).offset().top + $(this).height();

          theElement.find('.ribbon-list div ul').fadeOut('fast');

          if (insideX && insideY) {
              $(this).attr('style', 'background-image: ' + $(this).css('background-image'));

              $(this).parent().width(elwidth);
              //$(this).children('ul').width(elwidth - 4);
              
              $(this).children('ul').fadeIn('fast');
          }
      });
      
      $childItem.parents().click(function(e) {
    	  _hiddenPopMenu(e);
      });
      
      function _hiddenPopMenu(e)
      {
    	  var $item = theElement.find('.ribbon-list div ul:visible');
    	  if($item==null) return;
    	  
    	  var isHidden = true;
    	  if(e!=null)
    	  {
    		  var $p=$item.parent();
        	  if($p==null || $p.offset()==null) return;
        	  
              var outsideX = e.pageX < $p.offset().left || e.pageX > $p.offset().left + $p.width();
              var outsideY = e.pageY < $p.offset().top || e.pageY > $p.offset().top + $p.height();
              
              if (outsideX || outsideY){
              }
              else{isHidden=false;}
    	  }
    	  
    	  if (isHidden) {
        	  $item.each(function() {
                  $(this).fadeOut('fast');
              });
              $childItem.css('background-image', '');
          }
      }     
      
      $(window).blur(function(){_hiddenPopMenu();})
      if($.browser.msie){//qiaoxufeng:IE is so different to others,hh~    	 
    	  $('iframe').focus(function(){_hiddenPopMenu();});
      }
      
      return $(this);
    },
    
    $.fn.commonBar = function(settings) {
        settings = $.extend( settings || {});
        
        var theElement = this;
        theElement.addClass("commonBar");
        
        //seperator
        theElement.children().each(function(){
        	if($(this).find('label').length>0){
        		$(this).css('background-color','white');
        		return;
        	}
        	
			var text = $.trim($(this).text());				
			if(text=='-'){
				$(this).html('');
				$(this).addClass('separator');					
			}else{
				// Hover events
				$(this).mouseover( function() {
		        	theElement.find('LI.hover').removeClass('hover');
					$(this).addClass('hover');
				}).mouseout( function() {
					theElement.find('LI.hover').removeClass('hover');
				}).click(function(){
					if(settings.commandClick){
						var href = $(this).find('A').attr('href');
						if(href!=null && href.lastIndexOf('#')>0)
						{
							href=href.substr(href.lastIndexOf('#'));
						}
						settings.commandClick(href.substr(1));
					}
				});	
			}
		});               
		
		return theElement;
    }
    
})(jQuery);